from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_device_editor_dialog import Ui_Dialog_device_editor
import time
from database.db import Database

class Device_editor_signal(QObject):
    save_to_manger = Signal(dict)



class Device_editor_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Device_editor_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_device_editor()
        self.ui.setupUi(self)
        self.setWindowTitle('编辑设备')

        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Device_editor_signal()

        self.data = {}
        self.device_id = -1

        self.ui.pushButton_ok.clicked.connect(self.slot_save)


    def slot_show(self,device_id:int):
        self.device_id = device_id
        # 数据库 读取数据
        db = Database()
        fields_list =[
            'name','ip','port','user','password',
            'position','supplier','model','info'
        ]
        condition = f'id = {device_id}'
        s_data = db.select('devices',fields_list,condition)
        db.close_conn()

        data = {
            'name':s_data[0][0],
            'ip':s_data[0][1],
            'port':s_data[0][2],
            'user':s_data[0][3],
            'password':s_data[0][4],
            'position':s_data[0][5],
            'supplier':s_data[0][6],
            'model':s_data[0][7],
            'info':s_data[0][8],
        }
        self.data = data
        self.ui.lineEdit_name.setText(data['name'])
        self.ui.lineEdit_ip.setText(data['ip'])
        self.ui.lineEdit_port.setText(data['port'])
        self.ui.lineEdit_user.setText(data['user'])
        self.ui.lineEdit_password.setText(data['password'])
        self.ui.lineEdit_position.setText(data['position'])
        self.ui.lineEdit__supplier.setText(data['supplier'])
        self.ui.lineEdit_model.setText(data['model'])
        self.ui.textEdit_info.setText(data['info'])
        self.show()

    def slot_save(self):
        data = {
            'id'    :self.device_id,
            'name'  :self.ui.lineEdit_name.text(),
            'ip'    :self.ui.lineEdit_ip.text(),
            'port'  :self.ui.lineEdit_port.text(),
            'user'  :self.ui.lineEdit_user.text(),
            'password':self.ui.lineEdit_password.text(),
            'position':self.ui.lineEdit_position.text(),
            'supplier':self.ui.lineEdit__supplier.text(),
            'model' :self.ui.lineEdit_model.text(),
            'info':self.ui.textEdit_info.toPlainText()
        }

        self.signal.save_to_manger.emit(data)

        # 数据库存储
        cur_time = time.localtime()
        cur_time = time.strftime('%Y-%m-%d %H:%M:%S',cur_time)
        db = Database()
        set_expression = f"name={data['name']},supplier={data['supplier']},\
                        model={data['model']},position={data['position']},\
                        ip={data['ip']},port={data['port']},user={data['user']},\
                        password={data['password']},info={data['info']},\
                        updated_time='{cur_time}'"
        fields_list = ['name','supplier','model','position',
                       'ip','port','user','password','info',
                       'updated_time']
        values =(
            data['name'],data['supplier'],data['model'],
            data['position'],data['ip'],data['port'],
            data['user'],data['password'],data['info'],
            cur_time
            )
        db.update('devices',fields_list,values,f"id={data['id']}")
        
        db.close_conn()

     

